package mx.iteso.comp.logo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;

public class Interface extends JFrame {
	

	@SuppressWarnings("serial")
	
		JTextArea history;
		JTextArea command;
		public static void main(String ... s)
		{
			Interface tortu = new Interface();
			tortu.repaint();
		}
		public Interface()
		{
			super("Compilador de Logo ITESO");
			this.setSize(1024,500);
			this.setPreferredSize(new Dimension(1024,500));
			this.setResizable(true);
			this.setVisible(true);
			this.setLayout(new BorderLayout());
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.getContentPane().setBackground(new Color(255,255,255));
			
			panelTuga panT = new panelTuga();
			panT.setBackground(new Color(255,255,255));
			
			panelTuga historial = new panelTuga();
			panT.setBackground(new Color (255,255,255));
			
			history = new JTextArea(5,10);
			history.setBackground(new Color(255,255,255));
			history.setEditable(true);
			JScrollPane jspanHist = new JScrollPane(history);
			command = new JTextArea();
			command.addKeyListener(new myKey(this, panT));
			command.setSize(800,50);

			JPanel panelcito = new JPanel();
			panelcito.setLayout(new BorderLayout());
			panelcito.add(jspanHist, BorderLayout.PAGE_START);
			panelcito.add(command, BorderLayout.PAGE_END);
					
			this.getContentPane().add(panT);
			this.add(panelcito, BorderLayout.PAGE_END);
			
			this.pack();
		}
		
	}
	@SuppressWarnings("serial")
	class panelTuga extends JPanel
	{
		private Image tuga;
		Puntos posActual;
		public panelTuga()
		{
			super();
			setBackground(new Color(10,10,243));
			this.setLayout(null);
			this.setLocation(0,0);
			tuga = this.getToolkit().getImage("src/turti.png");
			posActual = Conector.add(new Puntos(Conector.centerX,Conector.centerY));
		}
		public void paint(Graphics g)
		{
			   super.paintComponent(g);
			   Graphics2D g2d = (Graphics2D)g;

			   posActual = new Puntos(Conector.centerX,Conector.centerY);
			   for (int i = 0 ; i < Conector.getPuntos().size(); i++)
					{
						Puntos siguiente = Conector.getPuntos().get(i);
				   		g2d.setColor(siguiente.getLinecolor());
				   		g2d.setStroke(new BasicStroke(siguiente.getSize()));
				   		if (siguiente.isPenDown())
				   		{
	//dibujar tortuga
				   			g2d.drawArc(siguiente.X()-siguiente.getRadius()/2, siguiente.Y()-siguiente.getRadius()/2, siguiente.getRadius(),siguiente.getRadius(), siguiente.getAngleStart(), siguiente.getAngleEnd());
					   		g2d.drawLine(posActual.X(), posActual.Y(), siguiente.X(), siguiente.Y());
						}
						posActual = siguiente;
					}
	//girar tortuga
			     AffineTransform origXform = g2d.getTransform();
			     AffineTransform newXform = (AffineTransform)(origXform.clone());
			     newXform.rotate(Math.toRadians(Conector.getTurtleAngle()), posActual.X(), posActual.Y());
			     g2d.setTransform(newXform);
			     
				g2d.drawImage(tuga, posActual.X()-tuga.getWidth(this)/2, posActual.Y()-tuga.getHeight(this)/2, this);		     
			    g2d.setTransform(origXform);


		}
		public void update(Graphics g)
		{
			paint(g);
		}
		
	}

	class myKey implements KeyListener
	{
		Interface t;
		panelTuga panT;
		public myKey(Interface t, panelTuga panT)
		{
			this.t = t;
			this.panT = panT;
		}
		public void keyReleased(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER )
			{
				t.command.setText("");	
			}
			t.repaint();
		}
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER )
			{			
				t.history.setText(t.history.getText()+"\n\r"+Compiler.Compiler(t.command.getText().toUpperCase()));
			}
		}
		public void keyTyped(KeyEvent e) {}	


}
